import os
import time

import requests


def main(url_01):
    resp = requests.get(url_01)
    resp2 = resp.json()
    cids = resp2.get('data').get('novel').get('items')
    for cid in cids:
        # print(cid.get('title'), '------------', cid.get('cid'))
        get_directory(cid.get('cid'))
    resp.close()


def get_directory(cid):
    url_02 = 'https://dushu.baidu.com/api/pc/getChapterContent?data={"book_id":"4306063500","cid":"4306063500|%s","need_bookinfo":1}' % cid
    resp_02 = requests.get(url_02, headers=header)
    data = resp_02.json()
    # print(data)
    title = data.get('data').get('novel').get('chapter_title')
    print(title)
    content = data.get('data').get('novel').get('content')
    with open(title, mode='w', encoding='utf-8') as f:
        f.write(content)
        f.flush()
    resp_02.close()


if __name__ == '__main__':
    start = time.time()
    header = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0.0"
    }
    folder_path = r"E:\PyCharm 2022.3.3\projects\pyDemo\main\py知识点\并发编程\novel"

    os.chdir(folder_path)

    url = 'https://dushu.baidu.com/api/pc/getCatalog?data={"book_id":"4306063500"}'

    main(url)
    end = time.time()
    print("运行时间>>> ", end - start)  # 39.6002995967865
